package com.xingchi.tornado.security.enums;

import com.xingchi.tornado.anno.Dict;
import com.xingchi.tornado.basic.BaseEnum;
import com.xingchi.tornado.security.model.MobileAuthenticationToken;
import com.xingchi.tornado.security.model.UsernamePasswordAuthenticationToken;
import com.xingchi.tornado.security.model.WxMiniProgramAuthenticationToken;
import lombok.Getter;
import org.springframework.security.core.Authentication;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 登录类型枚举
 *
 * @author xingchi
 * @date 2024/9/29 16:56
 */
@Getter
@Dict(value = "LOGIN_TYPE", desc = "登录类型枚举", name = "登录类型")
public enum LoginTypeEnum implements BaseEnum {

    USERNAME_PASSWORD(1, "用户名密码登录", UsernamePasswordAuthenticationToken.class),
    MOBILE_CODE(2, "手机号验证码登录", MobileAuthenticationToken.class),
    WX_MINI(3, "微信小程序登录", WxMiniProgramAuthenticationToken.class),

    ;
    private final Integer code;
    private final String desc;
    private final Class<? extends Authentication> clazz;

    LoginTypeEnum(Integer code, String desc, Class<? extends Authentication> clazz) {
        this.code = code;
        this.desc = desc;
        this.clazz = clazz;
    }

    private static final Map<Integer, LoginTypeEnum> CODE_MAPPINGS = Arrays.stream(LoginTypeEnum.values())
            .collect(Collectors.toMap(LoginTypeEnum::getCode, Function.identity()));

    public static LoginTypeEnum getInstance(Integer code) {
        return CODE_MAPPINGS.get(code);
    }

    public static LoginTypeEnum getInstanceOrDefault(Integer code, LoginTypeEnum defaultEnum) {
        return CODE_MAPPINGS.getOrDefault(code, defaultEnum);
    }

    public static boolean exists(Integer code) {
        return CODE_MAPPINGS.containsKey(code);
    }

}
